package order.orderdetail.view;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.ResultSet;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;

import order.orderdetail.model.OrderDetailModel;

import model.Employee;
import model.Role;
import net.proteanit.sql.DbUtils;
import admin.employee.view.EmployeeModel;

public class OrderDetailView extends JPanel {
	private static final long serialVersionUID = 1L;
	private final String NEW = "New";
	private final String UPDATE = "Update";
	private final String DELETE = "Delete";
	private final String SAVE = "Save";
	private final String CANCEL = "Cancel";
	
	private JTable tableOrderDetail = null;
	private OrderDetailModel orderDetailModel;
	
	JPanel orderDetailButtonPanel;
	JPanel orderDetailTextPanel;
	JPanel orderDetailTablePanel;
	
	private boolean b=false;//boolean value for enable disable Components function
	private boolean orderDetailIsUpdateButton = false;//variable for save button action
	private boolean orderDetailIsNewButton = false;//variable for save button action
	private int orderId;//orderId for detail table
	
	private JButton orderDetailNewB;
	private JButton orderDetailUpdateB;
	private JButton orderDetailDeleteB;
	private JButton orderDetailSaveB;
	private JButton orderDetailCancelB;
	
	private JTextField orderDetailIdTF;
	private JTextField order_IdTF;
	private JTextField drink_IdTF;
	private JTextField drinkNameTF;
	private JTextField drinkPriceTF;
	private JTextField quantityTF;
	
	private ButtonActionListener buttonActionListener;
	private TableMouseAdapter tableMouseAdapter;

	public OrderDetailView() {
		buttonActionListener = new ButtonActionListener();
		tableMouseAdapter = new TableMouseAdapter();
		orderDetailModel = new EmployeeModel();
		initConponents();
		update();		
	}
	
	public void update(){
		updateView();
	}

	private void updateView() {
		ResultSet rs = null;
		rs = orderDetailModel.employeeDao.getEmployeeResultSet();
		if(rs != null){
			tableOrderDetail.setModel(DbUtils.resultSetToTableModel(rs));
		}else { JOptionPane.showMessageDialog(null, "EmployeeView rs fault"); }		
	}

	private void initConponents() {
		buttonPanel = new JPanel();
		textPanel = new JPanel();
		tablePanel = new JPanel();
		
		//***ButtonPanel		
		add(buttonPanel);
		newB = new JButton(NEW); //new button
		newB.setActionCommand(NEW);
        newB.addActionListener(buttonActionListener);
		buttonPanel.add(newB);

		updateB = new JButton(UPDATE);
		updateB.setActionCommand(UPDATE);
        updateB.addActionListener(buttonActionListener);
		buttonPanel.add(updateB);

		deleteB = new JButton(DELETE);
		deleteB.setActionCommand(DELETE);
        deleteB.addActionListener(buttonActionListener);
		buttonPanel.add(deleteB);

		saveB = new JButton(SAVE);
		saveB.setActionCommand(SAVE);
        saveB.addActionListener(buttonActionListener);
        saveB.setEnabled(false);
		buttonPanel.add(saveB);
		
		cancelB = new JButton(CANCEL);
		cancelB.setActionCommand(CANCEL);
        cancelB.addActionListener(buttonActionListener);
        cancelB.setEnabled(false);
		buttonPanel.add(cancelB);
		
		buttonPanel.setLayout(new GridLayout(5, 1));
		buttonPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Employee Actions"));
		//***end of ButtonPanel
		
		//***TextPanel		
		add(textPanel);
		JLabel label = new JLabel("ssn :"); textPanel.add(label);
		ssnTF = new JTextField(); textPanel.add(ssnTF);
		label = new JLabel("name :"); textPanel.add(label);
		nameTF = new JTextField(); textPanel.add(nameTF);
		label = new JLabel("age :"); textPanel.add(label);
		ageTF = new JTextField(); textPanel.add(ageTF);
		label = new JLabel("gender :"); textPanel.add(label);
		genderTF = new JTextField(); textPanel.add(genderTF);
		label = new JLabel("username :"); textPanel.add(label);
		usernameTF = new JTextField(); textPanel.add(usernameTF);
		label = new JLabel("password :"); textPanel.add(label);
		passwordTF = new JTextField(); textPanel.add(passwordTF);
		label = new JLabel("classification :"); textPanel.add(label);
		classificationTF = new JTextField(); textPanel.add(classificationTF);
		textPanel.setLayout(new GridLayout(7, 2));
		textPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Employee Detail"));
		//***end of TextPanel
		
		//***TablePanel
		add(tablePanel);
		
		tableOrderDetail = new JTable();
		tableOrderDetail.addMouseListener(tableMouseAdapter);
		JScrollPane scrollPane = new JScrollPane( tableOrderDetail);
		tablePanel.add(scrollPane);
		//scrollPane.add(tableEmployee);
		tablePanel.setBorder(javax.swing.BorderFactory.createTitledBorder("Employee Table"));
		//***end of TablePanel
		edComponents(textPanel, false);//set disable textPanel components at the beginning
		
	}//end initComponents() 
	
	//sets enable or disable all components in a container
	private void edComponents(Container container, boolean b) {
		Component[] components = container.getComponents();
		for (Component component : components) {
			component.setEnabled(b);
			if (component instanceof Container) {
				edComponents((Container)component, b);
			}
		}			
	}
	
	//set correct enable disable button Panel components
	private void edComponents(Container container, String cmd){
		switch(cmd){
		case NEW:
			newB.setEnabled(false);
			updateB.setEnabled(false);
			deleteB.setEnabled(false);
			saveB.setEnabled(true);
			cancelB.setEnabled(true);
			break;
		case UPDATE:
			newB.setEnabled(false);
			updateB.setEnabled(false);
			deleteB.setEnabled(false);
			saveB.setEnabled(true);
			cancelB.setEnabled(true);
			break;
		case DELETE:
			//handle delete
			//JOptionPane.showMessageDialog(null, "Delete button pressed");
			break;
		case SAVE:
			newB.setEnabled(true);
			updateB.setEnabled(true);
			deleteB.setEnabled(true);
			saveB.setEnabled(false);
			cancelB.setEnabled(false);
			break;
		case CANCEL:
			newB.setEnabled(true);
			updateB.setEnabled(true);
			deleteB.setEnabled(true);
			saveB.setEnabled(false);
			cancelB.setEnabled(false);
			break;
		}
	}
			
    class TableMouseAdapter extends MouseAdapter {
		@Override
		public void mouseClicked(MouseEvent e) {
			//super.mouseClicked(e);
			if (tableOrderDetail.isEnabled()){//only work when tableEmployee is enabled
				System.out.println("mouse clicked on table");
				try {
					int row = tableOrderDetail.getSelectedRow();
					u = tableOrderDetail.getModel().getValueAt(row, 4).toString();
					p = tableOrderDetail.getModel().getValueAt(row, 5).toString();
					setTableRowToTextField(u, p);
				} catch (Exception e2) {
					System.out.println("Mouse listener Exception!");
				}
			}
		}    	
    }
    
	class ButtonActionListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			String cmd = e.getActionCommand();
			switch (cmd) {
			case NEW:
				b=!b;//if its true; set false....& vice versa 
				edComponents(textPanel, b);//enable disable components in container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);//enable disable components in container				
				System.out.println("NEWtbn in employee view");
				isNewButton = ! isNewButton;//set isNewButton opposite state
				emptyTextField();
				break;
			case UPDATE:
				if (!ssnTF.getText().isEmpty()) {//only work if there is to update
					b = !b;// if its true; set false....& vice versa
					edComponents(textPanel, b);// enable disable components in container
					ssnTF.setEnabled(false);//shouldnt change ssn number
					edComponents(tablePanel, !b);
					edComponents(buttonPanel, cmd);// enable disable components in container
					System.out.println("UPDATEbtn in employee view ," + b);
					isUpdateButton = ! isUpdateButton;//set isUpdateButton opposite state					
				}else {
					JOptionPane.showMessageDialog(null, "please click first on some item!!");
				}
				break;
				
			case DELETE:
				if (!ssnTF.getText().isEmpty()) {//only work if there is to delete
					edComponents(buttonPanel, cmd);// enable disable components in container and handle delete button
					System.out.println("DELETEbtn in employee view");
					orderDetailModel.employeeDao.deleteEmployee(ssnTF.getText());
					updateView();
				}else{
					JOptionPane.showMessageDialog(null, "please click first on some item!!");
				}
				break;
				
			case SAVE:
				b=!b;//if its true; set false....& vice versa 
				edComponents(textPanel, b);//enable disable components in container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);//enable disable components in container
				//decide action for save button (NewButton or UpdateButton ?)
				if(isUpdateButton){
					isUpdateButton = !isUpdateButton;
					System.out.println("save for Update Button");
					orderDetailModel.employeeDao.updateEmployee(
							( ssnTF.getText() ),
							( nameTF.getText() ),
							( ageTF.getText() ),
							( genderTF.getText() ),
							( usernameTF.getText() ),
							( passwordTF.getText() ),
							( classificationTF.getText() )	);
					updateView();
				}
				//decide action for save button (NewButton or UpdateButton ?)
				if(isNewButton){
					isNewButton = !isNewButton;
					System.out.println("save for New Button");
					orderDetailModel.employeeDao.newEmployee(
							( ssnTF.getText() ),
							( nameTF.getText() ),
							( ageTF.getText() ),
							( genderTF.getText() ),
							( usernameTF.getText() ),
							( passwordTF.getText() ),
							( classificationTF.getText() )	);
					updateView();
				}
				break;
				
			case CANCEL:
				isNewButton = !isNewButton;
				isUpdateButton = !isUpdateButton;
				b=!b;//if its true; set false....& vice versa 
				edComponents(textPanel, b);//enable disable components in container
				edComponents(tablePanel, !b);
				edComponents(buttonPanel, cmd);//enable disable components in container				
				System.out.println("CANCELbtn in employee view");
				if(u != null || p != null){
					setTableRowToTextField(u, p);
				}
				break;

			default:
				break;
			}
		}    	
    }//end ButtonActionListener class

	private void setTableRowToTextField(String userName, String password) {
		Employee tmpEmployee = orderDetailModel.employeeDao.getUser(userName, password);
		if(tmpEmployee != null){
			System.out.println(userName);
			ssnTF.setText(Integer.toString(tmpEmployee.getSsn()));
			nameTF.setText(tmpEmployee.getName());
			ageTF.setText(Integer.toString(tmpEmployee.getAge()));
			genderTF.setText(tmpEmployee.getGender());
			usernameTF.setText(userName);
			passwordTF.setText(password);
			int c; if(tmpEmployee.getClassification()==Role.ADMIN) c= 2; else c= 1;
			classificationTF.setText( Integer.toString(c) );
		}
		
	}

	private void emptyTextField() {
		ssnTF.setText("");
		nameTF.setText("");
		ageTF.setText("");
		genderTF.setText("");
		usernameTF.setText("");
		passwordTF.setText("");
		classificationTF.setText("");		
	}

}
